/*
 * @Author: xuetao 3168581151@qq.com
 * @Date: 2025-01-28
 * @LastEditors: xuetao 3168581151@qq.com
 * @LastEditTime: 2025-02-08
 * @Description: 联系人本地储存
 */
import 'package:chat_client/models/apply.dart';
import 'package:chat_client/models/contact.dart';
import 'package:chat_client/repositories/hive_repository.dart';
import 'package:hive/hive.dart';

/// 联系人本地储存
class ContactRepository {
  // 单例
  ContactRepository._internal() {
    _initContactBox();
    _initApplyBox();
  }

  static final ContactRepository _instance = ContactRepository._internal();
  static ContactRepository get instance => _instance;

  // hive 实例
  final _hive = HiveService.instance;

  // 打开数据库
  late final Box _contactBox;
  late final Box _applicationBox;

  Future<void> _initContactBox() async {
    if (!Hive.isBoxOpen('contactBox')) {
      _contactBox = await _hive.openBox('contactBox');
    }
  }

  Future<void> _initApplyBox() async {
    if (!Hive.isBoxOpen('applyBox')) {
      _applicationBox = await _hive.openBox('applyBox');
    }
  }

  // 储存好友申请列表
  Future<void> storeApplyList(List<Apply> applyList) async {
    // 将每个 Apply 单独存储在不同的键下
    for (int i = 0; i < applyList.length; i++) {
      await _applicationBox.put(applyList[i].opponentId, applyList[i]);
    }
  }

  Future<List<Apply>> getAllApplyList() async {
    List<Apply> applyList = [];

    // 获取所有存储的键
    List<dynamic> keys = _applicationBox.keys.toList();

    // 根据键读取所有 Apply 实例
    for (var key in keys) {
      Apply? apply = _applicationBox.get(key);
      if (apply != null) {
        applyList.add(apply);
      }
    }

    return applyList;
  }

  // 储存单个好友申请
  Future<void> storeApply(Apply apply) async {
    print(apply);
    await _applicationBox.put(apply.opponentId, apply);
  }

  // 获取单个 Apply
  Future<Apply?> getApply(String key) async {
    var apply = _applicationBox.get(key);
    return apply;
  }

  // 修改单个 Apply的处理状态
  Future<Apply> updateApply(String key, int status) async {
    // 获取当前存储的 Apply
    Apply? currentApply = _applicationBox.get(key);

    if (currentApply != null) {
      // 更新status字段
      currentApply.status = status;
      // 将更新后的 Apply 存回 Hive
      await _applicationBox.put(key, currentApply);

      return currentApply; // 返回更新后的 Apply 实例
    } else {
      // 如果没有找到对应的 Apply，返回原来的 Apply 实例或报错
      throw Exception('Apply with key $key not found');
    }
  }

  // // 关闭applyBox
  // Future<void> closeApplyBox() async {
  //   await _applicationBox.close();
  // }

  // 储存联系人列表数据
  Future<void> setContactgetContactList(List<Contact> contactList) async {
    await _initContactBox();
    for (var contact in contactList) {
      await _contactBox.put(contact.userId, contact);
    }
  }

  // 获取本地联系人列表数据
  Future<List<Contact>> getContactList() async {
    await _initContactBox();
    List<Contact> contactList = [];

    // 获取所有存储的键
    List<dynamic> keys = _contactBox.keys.toList();

    // 根据键读取所有 Contact 实例
    for (var key in keys) {
      Contact? contact = _contactBox.get(key);
      if (contact != null) {
        contactList.add(contact);
      }
    }

    return contactList;
  }

  // 储存单个联系人数据
  Future<void> setContact(Contact contact) async {
    await _initContactBox();
    await _contactBox.put(contact.userId, contact);
  }

  // 获取单个联系人数据
  Future<Contact?> getContact(String userId) async {
    await _initContactBox();
    var contact = _contactBox.get(userId);
    return contact;
  }

  // 修改本地单个联系人数据
  Future<void> updateContact(Contact contact) async {
    await _initContactBox();
    await _contactBox.put(contact.userId, contact);
  }

  // 删除本地联系人数据
  Future<void> deleteContact(String userId) async {
    await _initContactBox();
    await _contactBox.delete(userId);
  }

  // 关闭contactBox
  Future<void> closeContactBox() async {
    await _contactBox.close();
  }
}
